<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/3
 * Time: 14:10
 */

namespace app\sports_admin\controller;

use app\exception\TeacherException;
use think\Request;
use app\sports_admin\model\Teacher as TeacherModel;
use app\sports_admin\service\Teacher as TeacherService;
use app\sports_admin\model\Homework;
use app\sports_admin\service\HomeWork as HomeworkService;
use app\sports_admin\model\HomeworkComment as HomeworkCommentModel;
class Teacher extends Base
{
    protected $beforeActionList = [
        'isUsers'=>['only'=>'teacherInsert,teacherUpdate,teacherDelete']
    ];
    /**
     * 查看教师首页
     * @param $schoolId 学校id
     */
    public function index()
    {
        $schoolId = input('get.school_id');
        return view('index',compact('schoolId'));
    }
    /**
     * 教师列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public function teacherList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','schoolId']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $schoolId = intval($params['schoolId']);
        $result = TeacherModel::getTeacherInfo($page,$rows,$sort,$sortOrder,$searchName,$schoolId);
        return $result;
    }
    /**
     * 添加教师页面
    */
    public function add()
    {
        $schoolId = input('get.school_id');
        return view('add',compact('schoolId'));
    }
    /**
     * 添加教师页面
    */
    public function teacherInsert(Request $request)
    {
        if($request->isAjax()){
            return TeacherService::teacherInsert();
        }
    }
    /**
     * 教师信息编辑页面
     * @param $id 教师信息id
    */
    public function edit()
    {
        $id = input('get.id');
        $info = TeacherModel::with('school')->find($id);
        return view('edit',compact('info'));
    }
    /**
     * 修改教师信息
    */
    public function teacherUpdate(Request $request)
    {
        if($request->isAjax()){
            return TeacherService::teacherUpdate();
        }
    }
    /**
     * 删除教师操作
     * @param $id 教师信息id
     */
    public function teacherDelete(Request $request)
    {
        if($request->isAjax()){
            $id = $request->post('id');
            $bool = TeacherModel::destroy($id);
            if(!$bool){
                throw new TeacherException([
                    'msg'=>'教师信息删除失败',
                    'errorCode'=>400
                ]);
            }
            return json(['code'=>202,'msg'=>'删除成功']);
        }
    }
    //教师发布的所有作业的页面
    public function homeworkPage()
    {
        $data = Request::instance()->only('id');
        return view('homework_page',compact('data'));
    }
    //查看作业 作业列表
    public function homeworkList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','id','date']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $id = trim($params['id']);
        $date =  trim($params['date']);
        $result = Homework::homeworkList($page,$rows,$sort,$sortOrder,$id,$date);
        return $result;
    }
    //
    public function thisHomework()
    {
        $data = Homework::message();
        return view('homework_message',compact('data'));
    }
    /*
     * 家庭作业评论页
     * @param $homeworkId 作业id
     */
    public function homeworkComment()
    {
        $homeworkId = input('get.id');
        return view('comment',compact('homeworkId'));
    }
    /*
     * 家庭作业评论列表
     * @param $homeworkId 家庭作业id
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 开始时间
     * @param $end 结束时间
     */
    public function homeworkCommentList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','homeworkId','start','end']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $homeworkId = intval($params['homeworkId']);
        $start = trim($params['start']);
        $end = trim($params['end']);
        return HomeworkCommentModel::homeworkCommentList($page,$rows,$sort,$sortOrder,$searchName,$homeworkId,$start,$end);
    }
}
